package top.mask.mq.topics;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import top.mask.mq.utils.RabbitMQUtil;

/**
 * 声明主题交换机及相关队列
 * 消费者C1
 */
@Slf4j
public class ReceiveLogsTopic02 {
    //交换机的名称
    public static final String EXCHANGE_NAME = "topic_logs";

    //接收消息
    @SneakyThrows
    public static void main(String[] args) {
        Channel channel = RabbitMQUtil.getChannel();
        //声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
        //声明队列
        String queueName = "Q2";
        channel.queueDeclare(queueName,false,false,false,null);
        channel.queueBind(queueName,EXCHANGE_NAME,"*.*.rabbit");
        channel.queueBind(queueName,EXCHANGE_NAME,"lazy.#");
        log.info("等待接收消息...");

        //接收消息
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            log.info("ReceiveLogsTopic02日志打印接收到的消息:[{}]", new String(message.getBody(), "UTF-8"));
            log.info("接收队列:{},绑定键:{}",queueName,message.getEnvelope().getRoutingKey());
        };

        CancelCallback cancelCallback = consumerTag ->
                log.info("消息者取消消接口回调逻辑{[]}",consumerTag);
        channel.basicConsume(queueName,true,deliverCallback,cancelCallback);

    }
}
